package org.upfrost.converter;

public class BooleanNumberConverter implements Converter {

	@Override
	public Object convert(Object from, Class as) 
	{
		if (Boolean.class.equals(from.getClass()))
		{
			Boolean b = (Boolean) from;
			return b?1:0;
		}
		else
		{
			Number n = (Number)from;
			if (n.intValue()<=0)
				return Boolean.FALSE;
			else
				return Boolean.TRUE;
		}
	}

	@Override
	public boolean handles(Class from, Class as) {

		if (boolean.class.isAssignableFrom(from) && Number.class.isAssignableFrom(as))
			return true;
		if (boolean.class.isAssignableFrom(as) && Number.class.isAssignableFrom(from))
			return true;
		
		return false;
	}

}
